#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QMessageBox>
#include <QtSql/QSqlRecord>
#include <QtSql/QSqlError>
#include <QTableView>
#include <QVBoxLayout>
#include <QDebug>
#include <QMessageBox>
#include <QSqlQuery>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    //加载数据库驱动
    db = QSqlDatabase::addDatabase("QSQLITE");
    if(!db.isValid()){
        QMessageBox::critical(this,"Critical","数据库加载失败！");
        throw "数据库加载失败";
    }
    //设置连接的数据库
    db.setDatabaseName("./employe.db");
    //连接数据库
    bool ok = db.open();
    if(!ok){
        QMessageBox::critical(this,"Critical",db.lastError().text());
        throw "数据库连接失败";
    }
    //设置运行SQL语句
    model.setQuery("SELECT * FROM management");
    if(model.lastError().isValid()){
        qDebug() << model.lastError();
    }
    //显示查询结果
    ui->tableView->setModel(&model);


}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_cb_id_stateChanged(int arg1)
{
    Q_UNUSED(arg1);
    set_items();
}


void MainWindow::on_cb_name_stateChanged(int arg1)
{
    Q_UNUSED(arg1);
    set_items();
}


void MainWindow::on_cb_ein_stateChanged(int arg1)
{
    Q_UNUSED(arg1);
    set_items();
}


void MainWindow::on_cb_title_stateChanged(int arg1)
{
    Q_UNUSED(arg1);
    set_items();
}


void MainWindow::on_cb_birthday_stateChanged(int arg1)
{
    Q_UNUSED(arg1);
    set_items();
}


void MainWindow::on_cb_salary_stateChanged(int arg1)
{
    Q_UNUSED(arg1);
    set_items();
}


void MainWindow::on_cb_notes_stateChanged(int arg1)
{
    Q_UNUSED(arg1);
    set_items();
}

void MainWindow::set_items()
{
    QString items;

    if(ui->cb_id->isChecked()){
        items += ui->cb_id->text().toLower() + ",";
    }
    if(ui->cb_name->isChecked()){
        items += ui->cb_name->text().toLower() + ",";
    }
    if(ui->cb_ein->isChecked()){
        items += ui->cb_ein->text().toLower() + ",";
    }
    if(ui->cb_title->isChecked()){
        items += ui->cb_title->text().toLower() + ",";
    }
    if(ui->cb_birthday->isChecked()){
        items += ui->cb_birthday->text().toLower() + ",";
    }
    if(ui->cb_salary->isChecked()){
        items += ui->cb_salary->text().toLower() + ",";
    }
    if(ui->cb_notes->isChecked()){
        items += ui->cb_notes->text().toLower() + ",";
    }
    if(items.endsWith(",")){
        items[items.size()-1] = ' ';   //替换最后的逗号
    }
    model.setQuery(QString("SELECT %1 FROM management").arg(items));
    if(model.lastError().isValid()){
        qDebug() << model.lastError().text();
        throw model.lastError().text();
    }
    ui->tableView->setModel(&model);
}


void MainWindow::on_tableView_pressed(const QModelIndex &index)
{
    //获取鼠标选中的单条记录
    int row = index.row();
    QSqlRecord rec = model.record(row);

    ui->le_id->setText(rec.value(0).toString());
    ui->le_name->setText(rec.value(1).toString());
    ui->le_ein->setText(rec.value(2).toString());
    ui->le_title->setText(rec.value(3).toString());
    ui->le_birthday->setText(rec.value(4).toString());
    ui->le_salary->setText(rec.value(5).toString());
    ui->ple_notes->setPlainText(rec.value(6).toString());
}


void MainWindow::on_btn_add_clicked()
{
    int id = ui->le_id->text().toInt();
    QString name = ui->le_name->text();
    QString ein = ui->le_ein->text();
    QString title = ui->le_title->text();
    QString birthday = ui->le_birthday->text();
    double salary = ui->le_salary->text().toDouble();
    QString notes = ui->ple_notes->toPlainText();

    QSqlQuery query;
    query.prepare("INSERT INTO management " "VALUES(:id,:name,:ein,:title,:birthday,:salsry,:notes)");
    query.bindValue(0,id);
    query.bindValue(1,name);
    query.bindValue(2,ein);
    query.bindValue(3,title);
    query.bindValue(4,birthday);
    query.bindValue(5,salary);
    query.bindValue(6,notes);

    bool ok = query.exec();
    if(!ok){
        QMessageBox::warning(this,"Warning","插入失败");
    }
    //刷新
    set_items();
}


void MainWindow::on_btn_delete_clicked()
{
    bool ok;
    int id = ui->le_id->text().toInt(&ok);
    if(!ok){
        QMessageBox::warning(this,"Warning","ID 字段错误");
    }

    QSqlQuery query;
    //query.prepare("DELETE FROM management where id=:id");
    //query.addBindValue(":id",id);
    QString del = QString("DELETE FROM management where id=%1").arg(id);
    if(!query.exec(del)){
        QMessageBox::warning(this,"Warning",query.lastError().text());
        return ;
    }
    //刷新
    set_items();
}


void MainWindow::on_btn_update_clicked()
{
    on_btn_delete_clicked();
    on_btn_add_clicked();

}


void MainWindow::on_btn_select_clicked()
{
    bool ok;
    int id = ui->le_id->text().toInt(&ok);
    qDebug() << id;
    if(!ok){
        QMessageBox::warning(this,"Warning","ID 字段错误");
    }
    model.setQuery(QString("SELECT *FROM management where id=%1").arg(id));
}

